package ru.vsu.cs.pm.models;

import javax.persistence.*;
import java.io.Serializable;

/**
 * User: ohodyakov
 * Date: 23.12.2010
 * Time: 13:30:45
 */
@Entity
@Table(name = "item")
@NamedQueries({
        @NamedQuery(name = "item.get.children",
                query = "select i from Item i left join fetch i.user " +
                        "where i.user.id = :userId and " +
                        "i.parent.id = :parentId order by i.id"),
        @NamedQuery(name = "item.get.roots.by.user.id.and.item.type",
                query = "select i from Item i left join fetch i.user " +
                        "where i.user.id = :userId and " +
                        "i.isRootItem = true and " +
                        "i.type in (:itemTypes) " +
                        "order by i.id "),
        @NamedQuery(name = "item.get.by.user.id",
                query = "select i from Item i left join fetch i.user " +
                        "where i.user.id = :userId and " +
                        "i.isRootItem = :isRootItem and " +
                        "i.type in (:itemTypes) order by i.id")
})
public class Item implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "parent_id", nullable = false)
    private Item parent;

    @Column(name = "children_count", nullable = false)
    private Integer childrenCount;

    @Column(name = "is_root_item", nullable = false)
    private Boolean isRootItem;

    @Column(name = "title", nullable = false, length = 64)
    private String title;

    @Column(name = "description", nullable = true, length = 250)
    private String description;

    @Column(name = "item_type", nullable = false)
    private ItemType type;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Item getParent() {
        return parent;
    }

    public void setParent(Item parent) {
        this.parent = parent;
    }

    public String getTitle() {
        return title;
    }

    public Integer getChildrenCount() {
        return childrenCount;
    }

    public void setChildrenCount(Integer childrenCount) {
        this.childrenCount = childrenCount;
    }

    public Boolean isRootItem() {
        return isRootItem;
    }

    public void setRootItem(Boolean rootItem) {
        isRootItem = rootItem;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public ItemType getType() {
        return type;
    }

    public void setType(ItemType type) {
        this.type = type;
    }
}
